package com.wnxy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.hash.BloomFilter;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.common.itin.Limit;
import com.wnxy.admin.common.util.OrderUtil;
import com.wnxy.admin.dto.OrderDto;
import com.wnxy.admin.entity.Order;
import com.wnxy.admin.service.IReservationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wnxy.admin.service.IOrderService;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author (^･ｪ･^)Cat~
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private final IOrderService orderServiceImpl;
    private final IReservationService reservationServiceImpl;   // 预约管理

    private final BloomFilter<String> bloomFilter;

    public OrderController(IOrderService orderServiceImpl, IReservationService reservationServiceImpl, BloomFilter<String> bloomFilter){
        this.orderServiceImpl = orderServiceImpl;
        this.reservationServiceImpl = reservationServiceImpl;
        this.bloomFilter = bloomFilter;
    }

    @GetMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult page(@PathVariable Integer pageNum,
                               @PathVariable Integer pageSize,
                               @RequestParam(required = false) String keyword) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.and(wrapper ->
                    wrapper.like("order_id", keyword)
                            .or()
                            .like("customer_name", keyword)
            );
        }

        IPage<Order> page = Page.of(pageNum, pageSize);
        page = orderServiceImpl.page(page, queryWrapper);

        return ResponseResult.ok(page);
    }

    @PutMapping("edit")
    public ResponseResult edit(@RequestBody Order order){
        boolean isUpdate = orderServiceImpl.updateById(order);
        return ResponseResult.ok(isUpdate);
    }

    @GetMapping("queryOrderDetail")
    public ResponseResult queryOrderDetail(Long orderId) {
        Order order = orderServiceImpl.queryOrderDetail(orderId);
        return ResponseResult.ok(order);
    }

    @Limit(key = "saveOrder",permitsPerSecond = 1,timeout = 500,msg = "接口限流中:(")
    @PostMapping("saveOrder")
    public ResponseResult saveOrder(@RequestBody OrderDto orderDto) {
        // 0-通过MQTT获取 [cardId / customerId / paymentMethod[cardNumber] / cardId绑chargingPileNumber]
        Order order = orderUtil.generateOrder(orderDto);

        // 1-订单生成业务流
        orderServiceImpl.saveOrder(order);
        return ResponseResult.ok();
    }

    @PostMapping("/editOrder")
    public ResponseResult editOrder(@RequestBody OrderDto orderDto) {
        orderServiceImpl.editOrder(orderDto);
        return ResponseResult.ok();
    }

    @PostMapping("cardPay")
    public ResponseResult cardPay(Long cardId) {
        orderServiceImpl.cardPay(cardId);
        return ResponseResult.ok();
    }

    @DeleteMapping("/drop/{orderId}/{code}")
    public ResponseResult drop(@PathVariable Long orderId, @PathVariable Integer code) {
        orderServiceImpl.drop(orderId, String.valueOf(code));
        return ResponseResult.ok();
    }

    // -- 测试接口 API [2023-12-05] -----------------------------------------------------------

    @PostMapping("save")
    public ResponseResult addOrderAPI(Order order) {

        // 1-订单生成业务流
        orderServiceImpl.saveOrder(order);
        return ResponseResult.ok();
    }

    @Autowired
    OrderUtil orderUtil;
    @GetMapping("reservationQueryApi")
    public ResponseResult reservationQueryApi(Integer customerId) {
        List<Order> orders = orderUtil.queryNotOkOrderByCustomerId(customerId);
        return ResponseResult.ok(orders);
    }
}
